Good day C all!
i want to write a C program that is able to analyze a text document composed of an unknown amount of lines. The document is inserted from keyboard by the user. The program has to show the following statistics:
a. the total number of lines;
b. the total number of words;
c. the number of character.
For example, if the text provided as input is the following:
fatti non foste
per viver come bruti
ma per seguir virtute e canoscenza
the program has to print the following values:
Number of lines: 3
Number of words: 13
Average length of a word: 4.5
i've written the following code :
Code:
int main()
{
int i=0;
int word=0; int line=0; int count=0;
char arra[100];
char c =getchar();
while((c!= EOF) )
{
arra[i]=c;
count=count+1;
if ((arra[i]==' ' || arra[i]=='\n'))
word=word+1;
if (arra[i]=='\n')
++line;
i=i+1;
c=getchar();
}
printf("the number of words is :%d ",word);
printf("\nthe number of line is :%d",line);
printf("\nthe number of characters is :%d",count);
}
My questions are:
How can i define optimaly may array's size? i've put 100 because i had no better idea.
can I' print the global results without typing Ctrl+Z?
if there is any improvement for my code please show me!
thanks!!